<?php
namespace app\admin\controller;

use app\common\model\Withdraw as WithdrawModel;
use think\Exception;
use think\exception\ThrowableError;
use think\Request;
use app\admin\tool\Parser;
use app\common\model\Company as CompanyModel;
use think\Db;

class Withdraw extends Base{

    /**
     * 提现申请列表
     */
    public function index(){

        $this->data['title']  = '提现申请列表';
        $this->data['column'] =  $this->column();
        $this->data['model']  =  WithdrawModel::where([])->order('add_time desc')->paginate(20);
        $this->data['parser'] =  new Parser();

        return view('',$this->data);
    }


    /**
     * 财务打款后改变提现申请的状态
     * 同时显示申请公司的信息 以便财务是否确定 打款
     * @param  Request $request
     * @throws
     * @return \think\response\View
     */
    public function edit(Request $request){
        $id = $request->param('id');

        $model  = WithdrawModel::get($id);
        $company = CompanyModel::get($model->company_id);

        $this->data['title'] = '申请详情';
        $this->data['model'] = $model;
        $this->data['company'] = $company;

        return view('',$this->data);
    }


    /**
     * @param Request $request
     */
    public function update(Request $request){
        $post = $request->post();
        $data            = [];
        $data['id']      = $post['id'];
        $data['state']   = $post['state'];
        $data['message'] = $post['message'];


        if(!in_array($data['state'],[1,2])){
            $this->error("数据错误");
        }

        if($data['state'] != 2){
            unset($data['message']);
        }

        //后台仍然要做数据判断
        Db::startTrans();
        try{
            $withdraw = WithdrawModel::get($data['id']);
            if(in_array($withdraw->state,[1,2])){
                throw  new \Exception("不能对同一个申请重复操作"); //状态确定了 就不能改 要不然很麻烦，比如钱已经扣了，要遵循现实世界的规律，要改就走其他的渠道 比如重新提交个申请什么的，但是现在不管这个
            }
            $withdraw -> op_user = $this->admin->id;
            $withdraw -> update_time = time();
            $withdraw -> state = $data['state'];

            if($data['state'] == 2){
                $withdraw -> message = $data['message'];
                $withdraw -> save();
            }else{
                //如果是设置为已经打款 要检测公司的余额是否足够
                $company = CompanyModel::get($withdraw['company_id']);
                $company['balance']  = floatval($company['balance']);
                $withdraw['money']   = floatval($withdraw['money']);

                if($company['balance'] < $withdraw['money']){
                    throw new \Exception("提现金额大于余额");
                }

                //扣除余额
                $company['balance'] = $company['balance']-$withdraw['money'];
                $company->save();

                $withdraw -> save();
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success("操作成功");


    }

    protected function column(){
        return [
            'name'     =>  ['name'=>'姓名','fun'=>'text','options'=>[]],
            'card_num' =>  ['name'=>'银行卡号','fun'=>'text','options'=>[]],
            'bank'     =>  ['name'=>'开户行','fun'=>'text','options'=>[]],
            'money'    =>  ['name'=>'数额','fun'=>'text','options'=>[]],
            'state'    =>  ['name'=>'状态','fun'=>'translate','options'=>['map'=>['0'=>'申请中','1'=>'完成打款','2'=>'失败']]],
            'add_time' =>  ['name'=>'申请时间','fun'=>'datetime','options'=>[]]
        ];
    }
}

